﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace Toybox.CookieGetterSharp
{
  /// <summary>
  /// CookieGetterを意識せず簡易にクッキー取得
  /// </summary>
  public static class EasyGetter
  {
    /// <summary>
    /// ブラウザ名からクッキーを取得
    /// </summary>
    /// <param name="browser"></param>
    /// <param name="uri"></param>
    /// <param name="key"></param>
    /// <returns></returns>
    public static CookieCollection GetCookies(string browser, Uri uri, string key = null)
    {
      ICookieGetter[] iCookieGetters = CookieGetter.CreateInstances();

      foreach (ICookieGetter icg in iCookieGetters)
        if (browser == icg.ToString())
        {
          if (string.IsNullOrEmpty(key))
          {
            return icg.GetCookieCollection(uri);
          }
          else
          {
            CookieCollection cookies = new CookieCollection();
            Cookie cookie = icg.GetCookie(uri, key);

            if (cookie != null) 
              cookies.Add(cookie);

            return cookies;
          }
        }

      return null;
    }

    /// <summary>
    /// 有効なCookieGetterの名前を取得
    /// </summary>
    /// <returns></returns>
    public static string[] GetBrowserNames()
    {
      ICookieGetter[] iCookieGetters = CookieGetter.CreateInstances();

      string[] names = new string[iCookieGetters.Length];

      for (int n = 0; n < names.Length; n++)
        names[n] = iCookieGetters[n].ToString();

      return names;
    }
  }
}